﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RVEAd.Business.Model;

namespace RVEAd.Helpers
{
    internal static class Extensions
    {
        public static Report.DailyReportDS ToDailyReportDS(this IEnumerable<AdPlan> adPlansList)
        {
            Report.DailyReportDS dailyReportDS = new Report.DailyReportDS();
            IEnumerable<int> clientIds = adPlansList.Select(i => i.ClientAd.ID).Distinct().ToList();


            foreach (var clientAdPlanId in clientIds)
            {
                var clientAdPlans = from adPlanItem in adPlansList
                                    where
                                        adPlanItem.ClientAd.ID == clientAdPlanId
                                    orderby
                                        adPlanItem.PlanDate
                                    select adPlanItem;

                var first = clientAdPlans.ElementAt(0);

                foreach (var day in Common.GetMonthDays(DateTime.Now))
                {

                    var adPlan = clientAdPlans.Where(i => i.PlanDate == day).FirstOrDefault();
                    var dayInfo = first.ClientAd.Client.Nume + Environment.NewLine + Environment.NewLine + first.ClientAd.FileName;

                    if (adPlan != null)
                    {
                        dailyReportDS.DailyReport.AddDailyReportRow(
                            dayInfo,
                            day,
                            adPlan.AM1,
                            adPlan.AM2,
                            adPlan.AM3,
                            adPlan.PM1,
                            adPlan.PM2,
                            Common.TranslateDayName(adPlan.PlanDate.DayOfWeek));
                    }
                    else
                    {
                        dailyReportDS.DailyReport.AddDailyReportRow(
                            dayInfo,
                            day,
                            false,
                            false,
                            false,
                            false,
                            false,
                            "D");
                    }
                }
            }
            return dailyReportDS;

        }
    }
}
